package com.qianqu.lib_base.utils;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.LinearInterpolator;

/**
 * ${description}
 *
 * @author weiqiliu
 * @version 1.0 2018/7/12
 */
public class AnimUtil {
    public static void horizontalAnimation(View view, int duration, int endAddress) {
        view.animate().cancel();
        view.animate().translationX(endAddress).setDuration(duration);
    }

    public static void verticalNormalAnimation(View view, int duration, int endAddress) {
        view.animate().cancel();
        view.animate().setStartDelay(0).translationY(endAddress).setDuration(duration).setInterpolator(new LinearInterpolator());
    }

    public static void orizontalDelayAnimation(final View view, final int duration, int endAddress, final int startDelay) {
        view.animate().cancel();
        view.animate().setStartDelay(0).translationX(endAddress).setDuration(duration).setInterpolator(new AccelerateInterpolator()).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view.animate().cancel();
                view.animate().setStartDelay(startDelay).translationX(0).setDuration(duration).setInterpolator(new AccelerateInterpolator());
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        }).start();
        ;
    }

    public static void verticalAnimation(View view, int duration, int endAddress) {
        view.animate().cancel();
        view.animate().translationY(endAddress).setDuration(duration).setInterpolator(new AccelerateInterpolator());
    }

    public static void verticalDelayAnimation(final View view, int duration, int endAddress, final int duration2, final int startDelay) {
        view.animate().cancel();
        view.animate().setStartDelay(0).translationY(endAddress).setDuration(duration).setInterpolator(new AccelerateInterpolator()).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view.animate().cancel();
                view.animate().setStartDelay(startDelay).translationY(0).setDuration(duration2).setInterpolator(new AccelerateInterpolator());
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        }).start();
        ;

    }

    public static void verticalDelayAnimation(final View view, int duration, int endAddress, final int duration2) {
        verticalDelayAnimation(view, duration, endAddress, duration2, 2000);
    }

    public static void verticalPropertyAnimation(View view, int duration, float startAddress, float endAddress) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationY", startAddress, endAddress);
        anim.setInterpolator(new AccelerateInterpolator());
        anim.setDuration(duration).start();
    }

    public static void scaleAnimation(View view, int duration, float scale) {
        view.animate().cancel();
        view.animate().scaleY(scale).scaleX(scale).setDuration(duration).setInterpolator(new AccelerateInterpolator());
    }

    public static void verticalGoneAnimation(final View view, final View goneView, int duration, float endAddress) {
        view.animate().cancel();
        view.animate().translationY(endAddress).setInterpolator(new AccelerateInterpolator()).setDuration(duration).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        }).start();
    }

    public static void verticalPropertyEndAnimation(final View view, int duration, float startAddress, float endAddress) {
        view.animate().translationY(endAddress).setInterpolator(new AccelerateInterpolator()).setDuration(duration).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.VISIBLE);
                AnimUtil.verticalPropertyAnimation(view, 400, view.getY(), 0);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        }).start();
    }

    public static void horizontalPropertyEndAnimation(final View view, int duration, float startAddress, float endAddress) {
        view.animate().translationX(endAddress).setInterpolator(new AccelerateInterpolator()).setDuration(duration).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        }).start();
    }

    public static void verticalPropertyEndAnimationDialog(final View view, int duration, float startAddress, float endAddress) {
        view.animate().translationY(endAddress).setInterpolator(new AccelerateInterpolator()).setDuration(duration).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        }).start();
    }

    public static void AlphaDelayAnimation(final View view, int duration, float end, final boolean b) {
        view.animate().cancel();
        view.animate().alpha(end).setDuration(duration).setInterpolator(new AccelerateInterpolator()).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (b) {
                    view.setVisibility(View.GONE);
                } else {
                    view.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        }).start();
        ;

    }

    public static void AlphaDelay2DelayAnimation(final View view, final View view2, final int duration, final float end,final boolean b) {
        view.animate().cancel();
        view.animate().alpha(end).setDuration(duration).setInterpolator(new AccelerateInterpolator()).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (b) {
                    view.setVisibility(View.GONE);
                    view2.setVisibility(View.VISIBLE);
                } else {
                    view.setVisibility(View.VISIBLE);
                    view2.setVisibility(View.GONE);
                }
                view2.animate().cancel();
                view2.animate().alpha((float) (1.0-end)).setDuration(duration).setInterpolator(new AccelerateInterpolator());
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        }).start();
        ;

    }

    public static void horizontalPropertyAnimation(View view, int duration, float startAddress, float endAddress) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationX", startAddress, endAddress);
        anim.setInterpolator(new AccelerateInterpolator());
        anim.setDuration(duration).start();
    }

    public static void horizontaslPropertyAnimation(View view, int duration, float startX, float startY, float endX, float endY) {
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationX", startX, endX);
        anim.setInterpolator(new AccelerateInterpolator());
        anim.setDuration(duration);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(view, "translationY", startY, endY);
        anim.setInterpolator(new AccelerateInterpolator());
        anim.setDuration(duration);
        set.playTogether(anim, anim2);
        set.start();
    }

    public static void alphaAnimation(View view, int duration, float alpha) {
        view.animate().cancel();
        view.animate().alpha(alpha).setDuration(duration).setInterpolator(new AccelerateInterpolator());
    }

    public static void scaleSAnimation(View view, int duration) {
        view.animate().cancel();
        view.animate().scaleX(0).scaleY(0).setDuration(duration).setInterpolator(new AccelerateInterpolator());
    }

    public static void scaleBAnimation(View view, int duration) {
        view.animate().cancel();
        view.animate().scaleX(1).scaleY(1).setDuration(duration).setInterpolator(new AccelerateInterpolator());
    }

    public static void rotateAnimation(final View view, final int duration, final float rotate) {
        view.animate().cancel();
        view.animate().setStartDelay(0).rotation(rotate).setDuration(duration).setInterpolator(new AccelerateInterpolator()).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view.animate().cancel();
                view.animate().setStartDelay(2000).rotation(0).setDuration(duration).setInterpolator(new AccelerateInterpolator());
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    public static void Animation(final View view, int duration, float startAddress, float sub_height) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationY", 1.0f, sub_height);
        anim.setInterpolator(new AccelerateInterpolator());
        anim.setDuration(duration);

        final int height = view.getHeight();
        AnimatorSet set = new AnimatorSet();
        ValueAnimator heightAnimator = ValueAnimator.ofInt(0, (int) sub_height);
        //2.为目标对象的属性变化设置监听器
        heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 3.为目标对象的属性设置计算好的属性值
                int animatorValue = (int) animation.getAnimatedValue();
                ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
                marginLayoutParams.height = height - animatorValue;
                Log.e("address1", height + "");
                Log.e("address2", animatorValue + "");
                Log.e("address3", marginLayoutParams.height + "");
//                marginLayoutParams.height=1500;
                view.setLayoutParams(marginLayoutParams);
            }
        });
        heightAnimator.setDuration(duration);
        heightAnimator.setTarget(view);
//        heightAnimator.start();
        set.playTogether(heightAnimator, anim);
        set.start();
    }

}
